package com.opgea.crm.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.opgea.crm.common.SetupsAttributeQualifier;
import com.opgea.crm.common.SetupsModuleQualifier;
import com.opgea.crm.common.vo.ActivityVo;
import com.opgea.crm.common.vo.DatePeriodVo;
import com.opgea.crm.domain.entities.SetupsValue;
import com.opgea.crm.domain.modal.JsonModelMap;
import com.opgea.crm.services.ActivityService;
import com.opgea.crm.services.QualifierService;
import com.opgea.crm.services.SetupService;
import org.apache.log4j.Logger;


@Controller
@RequestMapping("activityreports")
public class ActivityReportController {

	@Autowired
	SetupService setupService;
	@Autowired
	ActivityService activityService;
	@Autowired 
	QualifierService qualifierService;
	
	Logger logger = Logger.getLogger(ActivityReportController.class);
	
	@RequestMapping(method=RequestMethod.GET)
	public String show(HttpServletRequest request){

		return "ActivityReport";
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "actionTypes")
	public @ResponseBody
	Map<String, Object> actionTypes() {
		logger.info("ActivitySearchController >> actionType");
		List<SetupsValue> actionList = setupService.listSetupValue(SetupsModuleQualifier.Activity.ordinal(), SetupsAttributeQualifier.Action.ordinal());
		actionList.add(new SetupsValue(0l, "All"));
		return JsonModelMap.success().data(actionList);
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "statusTypes")
	public @ResponseBody
	Map<String, Object> statusTypes() {
		List<SetupsValue> actionList = setupService.listSetupValue(SetupsModuleQualifier.Activity.ordinal(),SetupsAttributeQualifier.Status.ordinal());
		actionList.add(new SetupsValue(0l, "All"));
		return JsonModelMap.success().data(actionList);
	}

	@RequestMapping(method = RequestMethod.GET, value = "priorityTypes")
	public @ResponseBody
	Map<String, Object> priorityTypes() {
		List<SetupsValue> actionList = setupService.listSetupValue(SetupsModuleQualifier.Activity.ordinal(),SetupsAttributeQualifier.Priority.ordinal());
		actionList.add(new SetupsValue(0l, "All"));
		return JsonModelMap.success().data(actionList);
	}	
	
	@RequestMapping(method = RequestMethod.GET, value = "searchPeriodTypes")
	public @ResponseBody
	Map<String, Object> searchPriodTypes() {
		return JsonModelMap.success().data(qualifierService.getSearchPeriodQualifiers());
	}	
	
	@RequestMapping(method=RequestMethod.GET ,value="showActivitiesDynamically")
	public @ResponseBody
	Map<String, Object> showDynamically(
			@RequestParam(required=false, value="periodFrom")String periodFrom,
			@RequestParam(required=false, value="periodTo")String periodTo,
			@RequestParam(required=false, value="statusType")String statusType,
			@RequestParam(required=false, value="priorityType")String priorityType,
			@RequestParam(required=false, value="activityType")String activityType
			){
		if(periodFrom != null || periodTo != null){
			String periodFromDate = periodFrom.substring(0, periodFrom.indexOf('T'));
			String periodToDate = periodTo.substring(0, periodTo.indexOf('T'));
			List<ActivityVo> activities = activityService.findReport(periodFromDate, periodToDate, statusType, priorityType, activityType);
			return JsonModelMap.success().data(activities);			
		}else{
			return null;
		}
		
	}
	
	@RequestMapping(method=RequestMethod.GET ,value="showAll")
	public @ResponseBody
	Map<String, Object> showAll(){
		List<ActivityVo> activities = activityService.findAll();
		System.out.println(activities.size());
		return JsonModelMap.success().data(activities);
	}
	
	@RequestMapping(method=RequestMethod.GET, value="showPeriodDate")
	public @ResponseBody 
	Map<String, Object> showPeriodDate(@RequestParam(required=true, value="periodCriteria")String periodCriteria){
		DatePeriodVo periodVo = activityService.findPeriod(periodCriteria);
		return JsonModelMap.success().data(periodVo);
	}
}
